SuaHDDenHangPanel = Ext.extend(AbstractPanel, {
	layout : {
        type : 'vbox',
        align : 'stretch'
    },
    height : 900,
    
    initComponent : function() {
    	this.formLabel = new Ext.form.Label({
			cls : 'main-title',
			text : lang.text.hd.suaHdDenHang.toUpperCase()
		});
		
		this.formLabelContainer = new Ext.Container({
			cls : 'field-align-center',
			items : [this.formLabel]
		});
		
		this.hdFieldSet = new HopDongInfoFieldSet({
			readOnly : true,
			allowInputMaHd : false,
			height : 170
		});
		
		this.phieuKhoThuFieldSet = this.initPhieuKhoThuFieldSet();
		this.denHangInfoFieldSet = this.initDenHangInfoFieldSet();
		var buttonBar = this.initButtonBar();
		
		this.detailContainer = new Ext.Container({
			width : 1000,
			layout : 'customizedlayout',
			height : 500,
			labelWidth : 80,
			items : [this.hdFieldSet, {xtype : 'spacer', height : 10},
		              this.phieuKhoThuFieldSet, {xtype : 'spacer', height : 10},
					  this.denHangInfoFieldSet, {xtype : 'spacer', height : 10}, buttonBar]
		});
    	this.items = [this.formLabelContainer, {xtype : 'spacer', height : 15}, this.detailContainer];
    	SuaHDDenHangPanel.superclass.initComponent.call(this);
    },
    
    initPhieuKhoThuFieldSet : function() {
    	this.tienCtyChiuTextField = new Ext.form.DisplayField({
    		name : "soTien",
			width : 250,
			style: 'text-align: right',
			fieldLabel : lang.text.hd.tienCtyChiu
    	});
    	
    	this.lyDoTextArea = new Ext.form.TextArea({
    		name : 'ghiChu',
    		width : 250,
    		fieldLabel : lang.text.lyDo
    	});
    	
    	var container = new Ext.Container({
    		layout : 'customizedlayout',
    		labelWidth : 120,
    		items : [this.tienCtyChiuTextField, {xtype : 'spacer', height : 10}, this.lyDoTextArea]
    	});
    	
    	return new Ext.form.FieldSet({
			title : lang.text.hd.phieuKhoThu,
			checkboxToggle: true,
			checkboxName : 'checkKhoThu',
			autoHeight : true,
			height : 145,
			collapsible: true,
			items : [container]
		});
    },
    
    initDenHangInfoFieldSet : function() {
    	this.label1 = new Ext.form.DisplayField({
    		fieldLabel : lang.text.hd.doiTruong,
            width : 200
		});
		
    	this.label2 = new Ext.form.DisplayField({
    		fieldLabel : lang.text.hd.nvBan1,
            width : 200
		});
		
    	this.label3 = new Ext.form.DisplayField({
    		fieldLabel : lang.text.hd.nvBan2,
            width : 200
		});
		
    	this.label4 = new Ext.form.DisplayField({
    		fieldLabel : lang.text.cty,
            width : 200
		});
    	
    	var leftCtn = new Ext.Container({
    		layout : 'customizedlayout',
    		labelWidth : 120,
    		width : 200,
			items : [this.label1, this.label2, this.label3, this.label4]
		});
    	
    	this.tiLeDoiTruong = new Ext.form.DisplayField({
			fieldLabel : lang.text.tiLe,
			width : 200
		});
    	
    	this.thanhTienDoiTruong = new Ext.form.DisplayField({
			fieldLabel : lang.text.thanhTien,
			width : 250
		});
    	
    	this.tiLeNv1 = new Ext.form.DisplayField({
			fieldLabel : lang.text.tiLe,
			width : 200
		});
    	
    	this.thanhTienNv1 = new Ext.form.DisplayField({
			fieldLabel : lang.text.thanhTien,
			width : 250
		});
    	
    	this.tiLeNv2 = new Ext.form.DisplayField({
			fieldLabel : lang.text.tiLe,
			width : 200
		});
    	
    	this.thanhTienNv2 = new Ext.form.DisplayField({
			fieldLabel : lang.text.thanhTien,
			width : 250
		});
    	
    	this.tiLeCty = new Ext.form.DisplayField({
			fieldLabel : lang.text.tiLe,
			width : 200
		});
    	
    	this.thanhTienCty = new Ext.form.DisplayField({
			fieldLabel : lang.text.thanhTien,
			width : 250
		});
    	
    	var centerCtn = new Ext.Container({
    		layout : 'customizedlayout',
    		labelWidth : 40,
    		width : 200,
			items : [this.tiLeDoiTruong, this.tiLeNv1,  
			         this.tiLeNv2, this.tiLeCty]
		});
    	
    	var rightCtn = new Ext.Container({
    		layout : 'customizedlayout',
    		labelWidth : 70,
    		width : 500,
			items : [this.thanhTienDoiTruong, this.thanhTienNv1, this.thanhTienNv2, this.thanhTienCty ]
		});
    	
    	return new Ext.form.FieldSet({
			title : lang.text.hd.denHangInfo,
			autoHeight : true,
			height : 150,
			collapsible: true,
			layout : 'hbox',
			items : [leftCtn, {xtype : 'spacer', width : 50}, centerCtn, {xtype : 'spacer', width : 50}, rightCtn]
		});
    },
    
    initButtonBar : function() {
    	this.luuBtn = new Ext.Button({
			width : 40,
			text : lang.text.luu
		});
		
		this.thoatBtn = new Ext.Button({
			width : 40,
			text : lang.text.thoat
		});
		
		return new Ext.Container({
			layout : 'hbox',
			items : [this.luuBtn, {xtype : 'spacer', width : 13}, this.thoatBtn]
		});
    },
    
    afterRender : function() {
    	this.submitButtons = [this.luuBtn];
    	this.phieuKhoThuFieldSet.on('collapse', this.doCollapse, this);
    	this.phieuKhoThuFieldSet.on('expand', this.doExpand, this);
    	this.phieuKhoThuFieldSet.on('afterrender', function() {
    		this.phieuKhoThuFieldSet.collapse();
    		var f = this.phieuKhoThuFieldSet.checkbox.focus(200);
    	}, this);
    	this.calculateFine();
    	SuaHDDenHangPanel.superclass.afterRender.call(this);
    },
    
    doCollapse : function() {
    	this.denHangInfoFieldSet.enable();
    },
    
    doExpand : function() {
    	this.tienCtyChiuTextField.setValue(this.hdFieldSet.tienConLaiTextField.getValue());
    	this.denHangInfoFieldSet.disable();
    },
    
    calculateFine : function() {
		this.hdFieldSet.loadHdInfoToForm(ApplicationManager.hdInfo);
		if (ApplicationManager.hdInfo.doiTruong == "" || ApplicationManager.hdInfo.doiTruong == null) {
			this.label2.setValue(ApplicationManager.hdInfo.nvBan1);
			this.tiLeNv1.setValue("50%");
			this.tiLeCty.setValue("50%");
			var value = ApplicationManager.hdInfo.tienConLai / 2;
			this.thanhTienNv1.setValue(Utility.formatThousandSeparator(value));
			this.thanhTienCty.setValue(Utility.formatThousandSeparator(value));
			return
		}
		this.label1.setValue(ApplicationManager.hdInfo.doiTruong);
		this.label2.setValue(ApplicationManager.hdInfo.nvBan1);
		this.label3.setValue(ApplicationManager.hdInfo.nvBan2);
		Ext.Ajax.request({
            url : 'phieuThu/getCompensationRates',
            method : 'GET',
            scope : this,
            success : function(response, opts){
                var result = Ext.decode(response.responseText);
                if (result.success) {
                	var rate = result.rate
                	this.tiLeDoiTruong.setValue(rate.doiTruong + "%"); 
                	this.tiLeNv1.setValue(rate.nv1 + "%");
                	this.tiLeNv2.setValue(rate.nv2 + "%");
			        this.tiLeCty.setValue(rate.cty + "%");
			        var tienConLai = ApplicationManager.hdInfo.tienConLai;
			        this.thanhTienDoiTruong.setValue(Utility.formatThousandSeparator(rate.doiTruong * tienConLai / 100));
			        this.thanhTienNv1.setValue(Utility.formatThousandSeparator(rate.nv1 * tienConLai / 100));
			        this.thanhTienNv2.setValue(Utility.formatThousandSeparator(rate.nv2 * tienConLai / 100));
			        this.thanhTienCty.setValue(Utility.formatThousandSeparator(rate.cty * tienConLai / 100));
				} else {
					this.ownerCt.notifyMessage({action : "error", msg : lang.msg.unableToRetrieveHdInfo})
				}
            }, 
            failure : function() {}
        });
    },
    
    save : function() {
		this.disableButtons();
		var params;
		var isKhoThu = !this.phieuKhoThuFieldSet.collapsed
		if (isKhoThu) {
			params = {
				'tienCtyChiu' : ApplicationManager.hdInfo.tienConLai	
			}
		} else {
			params = {
				'denDt' : Utility.getRawValueOfNumericField(this.thanhTienDoiTruong),
            	'denNv1' : Utility.getRawValueOfNumericField(this.thanhTienNv1),
            	'denNv2' : Utility.getRawValueOfNumericField(this.thanhTienNv2),
            	'denCty' : Utility.getRawValueOfNumericField(this.thanhTienCty),
            	'doiTruong' : ApplicationManager.hdInfo.doiTruong,
            	'nvBan1' : ApplicationManager.hdInfo.nvBan1,
            	'nvBan2' : ApplicationManager.hdInfo.nvBan2
			}
		}
		this.getForm().submit({
		    me : this,
		    url : 'phieuThu/suaHdDenHang',
            waitMsg: lang.text.saving,
            waitMsgTarget : true,
            params : params,
            success : function(action, response) {
            	this.enableButtons();
            	var result = Ext.decode(response.response.responseText);
            	if (result.success) {
            		ApplicationManager.callbackMsg = result.msg;
                	ApplicationManager.openMenu(ApplicationManager.denHangFrom);
                	ApplicationManager.callbackMsg = null;
                	ApplicationManager.hdInfo = null;
				} else {
					this.notifyMessage(result.msg);
				}
            },
            failure : function(action, response) {
            	this.enableButtons();
            },
            scope : this
        });
    }
});